package com.lsy.android.pingpang.tool;

import java.util.Timer;
import java.util.TimerTask;

public class MyTimer {

	public static final long INIT_TIME = 1 * 60;
	private long totalTime = INIT_TIME;// 单位秒
	private Timer timer;
	private TimerTask timerTask;
	private boolean isPause = false;
	private boolean running = false;

	private static MyTimer instance;

	public static MyTimer getInstance() {
		if (instance == null) {
			instance = new MyTimer();
		}
		return instance;
	}

	private MyTimer() {
		timer = new Timer();
		timerTask = new TimerTask() {
			@Override
			public void run() {
				if (!isPause) {
					totalTime--;
				}
			}
		};
	}
	
	public boolean isRunning(){
		return running;
	}

	public void initTotalTime() {
		totalTime = INIT_TIME;
	}

	public long getTotalTime() {
		return totalTime;
	}

	public String getStringTotalTime() {
		return totalTime / 60 + ":" + totalTime % 60;
	}

	public void start() {
		timer.schedule(timerTask, 0, 1000);
		running=true;
	}

	public void pause() {
		isPause = true;
	}

	public void goOn() {
		isPause = false;
	}

	public void stop() {
		pause();
		initTotalTime();
		timer.cancel();
		timer = null;
		running=false;
		instance=null;
	}

}
